package com.glela.talent.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author MaoZeYao
 *	便捷时间工具类
 */
public enum MyDateUtil {
	
	INSTANCE;
	
	private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 得到一年之后的时间 +1天到凌晨
	 * @return
	 * @throws ParseException 
	 */
	public String getYear(String args) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		Date getDate = sdf.parse(args);
		calendar.setTime(getDate);
		calendar.add(Calendar.YEAR, +1);
		calendar.add(Calendar.DAY_OF_MONTH, +1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 得到指定周  +1天到凌晨
	 * @param args
	 * @return
	 */
	public String getWeekDay(int args) {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(Calendar.WEEK_OF_YEAR, +args);
		calendar.add(Calendar.DAY_OF_MONTH, +1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 得到指定天数  +1天到凌晨
	 * @param args
	 * @return
	 */
	public String getDays(int args) {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, +args+1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return sdf.format(calendar.getTime());
	}
	
	/**
	 * 得到指定天数  +1天到凌晨
	 * @param args
	 * @return
	 * @throws ParseException 
	 */
	public String getAccurateDays(String startDate, int days) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sdf.parse(startDate));
		calendar.add(Calendar.DAY_OF_MONTH, + 7);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 得到指定天数  +1天到凌晨
	 * @param args
	 * @return StrDate
	 * @throws ParseException 
	 * @author gwq
	 */
	public String getDaysFromDate(String Date,int days) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sdf.parse(Date));
		calendar.add(Calendar.DAY_OF_MONTH, + days);
		return sdf.format(calendar.getTime());
	}
	
	/**
	 * 得到指定月份
	 * @param args
	 * @return
	 */
	public String getMonth(int args) {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, +args);
		calendar.add(Calendar.DAY_OF_MONTH, +1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return sdf.format(calendar.getTime());
	}
	
	/**
	 * 有效时间比较
	 * @param startDate
	 * @param endDate
	 * @return 
	 * @throws ParseException 
	 */
	public boolean betweenDate(String start, String end) throws ParseException{
		boolean status = false;
		Date now = new Date();
		Date startDate = sdf.parse(start);
		Date endDate = sdf.parse(end);
		Long newdate = now.getTime();
		// 当前时间大于开始时间     结束时间小于开始时间   表示在这个时间区
		if (startDate.getTime() <= newdate && endDate.getTime() >= newdate) {
			status = true;
		}
		return status;
	}
	
	/**
	 * 格式化时间
	 * @param args
	 * @return
	 * @throws ParseException
	 */
	public Date formatDate(String args) throws ParseException{
		return sdf.parse(args);
	}
	
	public Date formatDateLong(String times) throws ParseException{
		return sdf.parse(times);
	}
	
	public String formatString(Date date){
		return sdf.format(date);
	}
	
	/**
	 * 比对两个时间大小
	 * @param dateActive 激活码时间
	 * @param dateUser	  用户结束时间
	 * @return
	 * @throws ParseException 
	 */
	public boolean betweenTwoDate(String dateActive,String dateUser) throws ParseException{
		Date one = sdf.parse(dateActive);
		Date two = sdf.parse(dateUser);
		if(one.getTime() - two.getTime() > 0){
			return true;
		}
		return false;
	}
	
	public String getMinutes(String createDate, int minutes) throws ParseException{
		Date create = sdf.parse(createDate);
		return sdf.format(create.getTime()+(minutes*60*1000));
	}
	
	/**
	 * 获得历史的天数
	 * @param args
	 * @return
	 */
	public String getOldDays(int args) {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -args);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return sdf.format(calendar.getTime());
	}
	
	/**
	 * 获取前一天的23：59：59
	 * @param args
	 * @return
	 */
	public String getYesterday(Date now) {
		Calendar calendar = Calendar.getInstance();
		// Date date = new Date();
		calendar.setTime(now);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return sdf.format(calendar.getTime());
	}
	
}